package com.shopping.mall.core.module.diy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shopping.mall.auth.annotation.UnLogin;
import com.shopping.mall.base.http.Result;
import com.shopping.mall.core.module.diy.entity.Diy;
import com.shopping.mall.core.module.diy.service.DiyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "Diy")
@RestController
@RequestMapping("/diy")
public class DiyController {
    @Autowired
    private DiyService diyService;

    @UnLogin
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<IPage<Diy>> getDiyPage(
        @RequestParam(name = "current", defaultValue = "1") Integer current,
        @RequestParam(name = "size", defaultValue = "10") Integer size,
        Diy diy) {
        IPage<Diy> diyPage=diyService.getDiyPage(current,size,diy);
        return Result.OK(diyPage);
    }

    @ApiOperation("修改")
    @PutMapping
    public Result<Diy> update(@Validated(Diy.update.class) @RequestBody Diy diy) {
        diyService.updateById(diy);
        return Result.OK();
    }

    @ApiOperation("新增")
    @PostMapping
    public Result<Object> add(@Validated(Diy.insert.class) @RequestBody Diy diy) {
        diyService.save(diy);
        return Result.OK(diy.getId());
    }

    @UnLogin
    @ApiOperation("通过id查询")
    @GetMapping("/id")
    public Result<Diy> getById(@RequestParam String id){
        Diy diy=diyService.getById(id);
        return Result.OK(diy);
    }

    @UnLogin
    @ApiOperation("查询所有")
    @GetMapping
    public Result<List<Diy>> getAll(Diy diy){
        List<Diy> diyList=diyService.getAll(diy);
        return Result.OK(diyList);
    }

    @ApiOperation("通过id删除")
    @DeleteMapping("/id")
    public Result<Object> deleteById(@RequestParam String id){
        return diyService.deleteById(id);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/batch")
    public Result<Object> batchDelete(@RequestParam List<String> ids){
        return diyService.batchDelete(ids);
    }
}
